//
//  ProductImageViewController.m
//  AmazingShopping
//
//  Created by Tan on 4/27/13.
//  Copyright (c) 2013 Tan Hoang. All rights reserved.
//

#import "ProductImageViewController.h"

@interface ProductImageViewController ()

@end

@implementation ProductImageViewController

@synthesize imageArray, bigScrollView, smallScrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    imageThumbWidth = 40;
    [self.navigationItem setTitle:@"Images"];
    [bigScrollView setMaximumZoomScale:2.0];
    [bigScrollView setMinimumZoomScale:1.0];
    isZooming = NO;
    [self setData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBigScrollView:nil];
    [self setSmallScrollView:nil];
    [super viewDidUnload];
}

- (void)setData{
    CGFloat smallScrollViewWidth = 0;
    
    //Set for bigScrollView
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(bigScrollView.frame.size.width*i, 0, bigScrollView.frame.size.width, bigScrollView.frame.size.height)];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        NSString *imagePath = [imageArray objectAtIndex:i];
        NSData *imageData = [[NSData alloc]initWithContentsOfURL: [NSURL URLWithString:imagePath]];
        [imageView setImage:[UIImage imageWithData:imageData]];
        [bigScrollView addSubview:imageView];
    }
    
    [bigScrollView setContentSize:CGSizeMake(imageArray.count * bigScrollView.frame.size.width, bigScrollView.frame.size.height)];
    bigScrollView.showsHorizontalScrollIndicator = NO;
    bigScrollView.pagingEnabled = YES;
    
    //Set for smallScrollView
    for (int i = 0; i < imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((imageThumbWidth+10)*i, smallScrollView.frame.origin.y+15, imageThumbWidth, imageThumbWidth)];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        NSString *imagePath = [imageArray objectAtIndex:i];
        NSData *imageData = [[NSData alloc]initWithContentsOfURL: [NSURL URLWithString:imagePath]];
        [imageView setImage:[UIImage imageWithData:imageData]];
        
        UITapGestureRecognizer *imageTouch = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchInImage:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:imageTouch];
        
        [smallScrollView addSubview:imageView];
    }
    
    smallScrollViewWidth = (imageThumbWidth + 10) * imageArray.count;
    
    [smallScrollView setContentSize:CGSizeMake(smallScrollViewWidth, smallScrollView.frame.size.height)];
    smallScrollView.showsHorizontalScrollIndicator = NO;
    
    //Make scroll bar
    scrollBar = [[UIView alloc]initWithFrame:CGRectMake(0, smallScrollView.frame.size.height-10, imageThumbWidth, 5)];
    scrollBar.backgroundColor = [UIColor blackColor];
    [smallScrollView addSubview:scrollBar];

    //Set Position of smallScrollView to Center of Screen
    if (smallScrollViewWidth < smallScrollView.frame.size.width) {
        CGRect frame = smallScrollView.frame;
        frame.size.width = smallScrollViewWidth;
        frame.origin.x = smallScrollView.frame.origin.x + (smallScrollView.frame.size.width - frame.size.width)/2;
        smallScrollView.frame = frame;
    }  
    
}

- (void)setImageArray:(NSArray *)ia{
    if (ia.count > 0) {
        imageArray = [ia copy];
    }
}

- (void)touchInImage:(UITapGestureRecognizer *)gesture{
    isTouchInSmallScrollView = YES;
    CGPoint touchPoint = [gesture locationInView:smallScrollView];
    int offset = touchPoint.x / (imageThumbWidth + 10);
    CGFloat offsetX = offset * bigScrollView.frame.size.width;
    CGFloat offsetY = bigScrollView.frame.origin.y;
    [bigScrollView setContentOffset:CGPointMake(offsetX, offsetY) animated:YES];
    
    offsetX = offset * (imageThumbWidth+10);
    offsetY = smallScrollView.frame.size.height-10;
    
    [UIView animateWithDuration:0.3
                     animations:^{
                         scrollBar.frame = CGRectMake(offsetX, offsetY, imageThumbWidth, 5);
                     }];    
}

#pragma mark UIScrollView Delegates
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (!isTouchInSmallScrollView) {
        CGFloat offset = scrollView.contentOffset.x;
        CGFloat offsetX = (offset/320) * (imageThumbWidth+10);
        CGFloat offsetY = smallScrollView.frame.size.height-10;        
        [UIView animateWithDuration:0.3
                         animations:^{
                             scrollBar.frame = CGRectMake(offsetX, offsetY, imageThumbWidth, 5);
                         }];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int offsetBig = scrollView.contentOffset.x / bigScrollView.frame.size.width;
    int currentOffsetSmall = smallScrollView.contentOffset.x / (imageThumbWidth+10);
    int maxOffsetSmall = smallScrollView.frame.size.width / (imageThumbWidth + 10);

    //Scroll smallScrollView
    if (offsetBig >= currentOffsetSmall + maxOffsetSmall){
        [smallScrollView setContentOffset:CGPointMake((currentOffsetSmall + 1) * (imageThumbWidth+10), smallScrollView.frame.origin.y) animated:YES];

    }else if (offsetBig < currentOffsetSmall){
        [smallScrollView setContentOffset:CGPointMake( (currentOffsetSmall - 1) * (imageThumbWidth+10), smallScrollView.frame.origin.y) animated:YES];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    isTouchInSmallScrollView = NO;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    
    int offset = scrollView.contentOffset.x / scrollView.frame.size.width;
    isZooming = YES;
    return [scrollView.subviews objectAtIndex:offset];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    int offset = scrollView.contentOffset.x / scrollView.frame.size.width;
    CGSize boundsSize = scrollView.bounds.size;
    UIImageView *imageView = [[scrollView subviews]objectAtIndex:offset];
    CGRect contentsFrame = imageView.frame;
    
    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }
    
    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    imageView.frame = contentsFrame;
}

@end
